package com.qa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qa.common.Const;
import com.qa.common.ServerResponse;
import com.qa.dao.UserMapper;
import com.qa.pojo.User;
import com.qa.service.IUserService;
import com.qa.utils.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author
 * @Date 2020/2/26 21:59
 * @Desc
 **/
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public PageInfo<User> getByEmail(String userEmail, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> users = userMapper.getByUserEmail(userEmail);
        return new PageInfo<>(users);
    }

    @Override
    public User getById(Integer userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    /**
     * 更新用户信息
     *
     * @param user
     * @return
     */
    @Override
    public ServerResponse updateUser(User user) {
        int result = 0;
        if (StringUtils.isBlank(user.getPassword())) {
            // 不更新密码
            result = userMapper.updateByPrimaryKeyNoPassword(user);
        } else {
            user.setPassword(MD5Util.getSaltMD5(user.getPassword()));
            // 更新密码
            result = userMapper.updateByPrimaryKey(user);
        }
        if (result > 0) {
            return ServerResponse.createByErrorMessage("更新成功！");
        }
        return ServerResponse.createByErrorMessage("更新失败！");
    }

    @Override
    public ServerResponse deleteById(Integer userId) {
        int result = userMapper.deleteByPrimaryKey(userId);
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("删除成功！");
        }
        return ServerResponse.createByErrorMessage("删除失败！");
    }

    @Override
    public ServerResponse register(User user) {
        // 查询用户是否存在
        List<User> byUserEmail = userMapper.getByUserEmail(user.getEmail());
        if (byUserEmail.size() != 0 && byUserEmail != null) {
            return ServerResponse.createByErrorMessage("邮箱已经被注册！");
        }

        user.setPassword(MD5Util.getSaltMD5(user.getPassword()));
        user.setStatus(1);
        user.setRole(2);
        int result = userMapper.insert(user);
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("成功！请登录");
        }
        return ServerResponse.createByErrorMessage("失败！");
    }

    @Override
    public ServerResponse login(User user, HttpServletRequest request) {
        User user1 = userMapper.getByUserEmailAndStatus(user.getEmail());
        if (user1 == null) {
            return ServerResponse.createByErrorMessage("账户不存在！");
        }

        if (!MD5Util.getSaltVerifyMD5(user.getPassword(),user1.getPassword())) {
            return ServerResponse.createByErrorMessage("请检查密码！");
        }
        request.getSession().setAttribute(Const.CURRENT_USER,  user1);
        return ServerResponse.createBySuccess("登录成功！", user1);
    }

    @Override
    public int getCount(String beginTime, String endTime) {
        return userMapper.getCount(beginTime, endTime);
    }
}
